extends KinematicBody2D

const WALK_SPEED = 150
const PROJECTILE_BUFFER = 24
const COMMANDS = "Q - Reset\nF - Cast spell\nG - Next spell"

const FIREBALL = preload("res://Scenes/fireball.xscn")
const LIGHTNING = preload("res://Scenes/lightning.xscn")

var anim = ""
var facing = 1
var velocity = Vector2()
var stamina = 100
var dead = false

var selected_spell = -1
var spellbook = [{"name" : "fireball", "cost" : 10},
                 {"name" : "lightning", "cost" : 20}]

func _input(event):
    var valid = event.is_pressed() && !event.is_echo()
    var level = get_node("/root/global").level
    if event.is_action("reset") && valid:
        get_node("/root/global").goto_scene(get_parent().get_filename())
    elif dead:
        pass
    elif event.is_action("next_spell") && valid:
        next_spell()
    elif event.is_action("cast") && valid:
        cast()

func next_spell():
    selected_spell = (selected_spell + 1) % get_node("/root/global").spell_level
    var s = spellbook[selected_spell]
    get_parent().get_node("commands").get_node("spell_info").set_text(s["name"] + " - " + str(s["cost"]))

func cast():
    var s = spellbook[selected_spell]
    stamina = stamina - s["cost"]
    if s["name"] == "fireball":
        shoot_projectile(FIREBALL)
    elif s["name"] == "lightning":
        shoot_projectile(LIGHTNING)

func shoot_projectile(projectile_type):
    var p = projectile_type.instance()
    get_parent().add_child(p)
    if (facing == 0):
        p.direction = Vector2(0, -1)
        p.set_pos(get_pos() + Vector2(0, -PROJECTILE_BUFFER))
    elif (facing == 1):
        p.direction = Vector2(0, 1)
        p.set_pos(get_pos() + Vector2(0, PROJECTILE_BUFFER))
    elif (facing == 2):
        p.direction = Vector2(-1, 0)
        p.set_pos(get_pos() + Vector2(-PROJECTILE_BUFFER, 0))
    else:
        p.direction = Vector2(1, 0)
        p.set_pos(get_pos() + Vector2(PROJECTILE_BUFFER, 0))
    get_node("playersounds").play("shoot")

func _fixed_process(delta):
    var new_anim = anim
    
    velocity = Vector2(0, 0)
    if (Input.is_action_pressed("ui_up")):
        new_anim = "up"
        facing = 0
        velocity.y = -WALK_SPEED
    elif (Input.is_action_pressed("ui_down")):
        new_anim = "down"
        facing = 1
        velocity.y = WALK_SPEED        
    elif (Input.is_action_pressed("ui_left")):
        new_anim = "left"
        facing = 2
        velocity.x = -WALK_SPEED
    elif (Input.is_action_pressed("ui_right")):
        new_anim = "right"
        facing = 3
        velocity.x = WALK_SPEED

    get_parent().get_node("stamina").get_node("value").set_text(str(stamina) + " / 100")
    if (stamina <= 0):
        get_parent().get_node("losepanel").show()
        hide()
        clear_shapes()
        dead = true
    if (stamina < 0):
        stamina = 0

    var motion = velocity * delta

    move(motion)

    if (is_colliding()):
        var n = get_collision_normal()
        motion = n.slide(motion)
        velocity = n.slide(velocity)
        move(motion)

    if (new_anim != anim):
        anim = new_anim
        get_node("anim").play(anim)

func _ready():
    set_fixed_process(true)
    set_process_input(true)
    get_parent().get_node("commands").get_node("text").set_text(COMMANDS)
    next_spell()

